
package com.yuzhi.fine.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yuzhi.fine.R;
import com.yuzhi.fine.fragment.BufferKnifeFragment;
import com.yuzhi.fine.fragment.MainPagerFragment;
import com.yuzhi.fine.fragment.MemberFragment;
import com.yuzhi.fine.utils.UIUtil;

public class MainActivity extends BaseFragmentActivity {

    private static int tabIndex = 0;
    private static final String TAB_INDEX_KEY = "tabIndex";
    private static final String[] fragmentTags = new String[] {
            "HomeFragment", "ImFragment", "InterestFragment", "MemberFragment"
    };

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        if(savedInstanceState != null) {
            tabIndex = savedInstanceState.getInt(TAB_INDEX_KEY);
        }
        initView();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(TAB_INDEX_KEY, tabIndex);
        super.onSaveInstanceState(outState);
    }

    private void initView() {
        RadioGroup group = findViewById(R.id.group);

        // 点击效果
        for(int i = 0; i < group.getChildCount(); i++) {
            if(group.getChildAt(i) instanceof RadioButton) {
                UIUtil.addHomeTabClickEffect(group.getChildAt(i));
            }
        }

        // 点击切换
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int index;
                switch (checkedId) {
                    case R.id.foot_bar_home: index = 0; break;
                    case R.id.foot_bar_im: index = 1; break;
                    case R.id.foot_bar_interest: index = 2; break;
                    case R.id.main_footbar_user: index = 3; break;
                    default: index = 0; break;
                }
                showFragment(index);
            }
        });

        // 显示(首页Tab或者被恢复Tab)
        showFragment(tabIndex);
    }

    private void showFragment(int index) {
        // 隐藏旧Tab
        if(tabIndex != index) {
            Fragment prevFragment = fragmentManager.findFragmentByTag(fragmentTags[tabIndex]);
            if(prevFragment != null) {
                fragmentManager.beginTransaction().hide(prevFragment).commit();
            }
        }
        // 显示新Tab
        Fragment fragment = fragmentManager.findFragmentByTag(fragmentTags[index]);
        if(fragment != null && fragment.isAdded()) {
            fragmentManager.beginTransaction().show(fragment).commit();
        } else {
            fragment = instantFragment(index);
            fragmentManager.beginTransaction().add(R.id.fragment_container, fragment, fragmentTags[index]).commit();
        }
        tabIndex = index;
    }

    private Fragment instantFragment(int currIndex) {
        switch (currIndex) {
            case 0: return new MainPagerFragment();
            case 1: return new BufferKnifeFragment();
            case 2: return new BufferKnifeFragment();
            case 3: return new MemberFragment();
            default: return new MainPagerFragment();
        }
    }
}
